<?php
namespace common\modules\comment;

class ModuleInfo extends \common\core\modularity\ModuleInfo
{

    public function init()
    {
        parent::init();

        $this->id = 'comment';
        $this->name = '本地评论';
        $this->description = '本地评论模块';
        $this->version = '1.0.0';
        $this->icon = '<i class="fa fa-commenting"></i>';
        $this->has_backend = true;
        $this->has_frontend = false;
        $this->has_api = false;

        $this->is_system = false;

        $this->requireModules = 'user';
    }

    public function install()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%comment}}', [
            'id' => $this->primaryKey(),
            'parent_id' => $this->integer(),
            'reply_ids' => $this->string(128),
            'user_id' => $this->integer(),
            'user_nickname' => $this->string(20),
            'user_ip' => $this->string(64),
            'user_address' => $this->string(128),
            'object_type' => $this->string(20)
                ->notNull(),
            'object_id' => $this->integer()
                ->notNull(),
            'level' => $this->integer(),
            'score' => $this->integer(),
            'parse' => $this->smallInteger()
                ->defaultValue(1), // 解析方式:1 富文本 2 语音
            'content' => $this->text(),
            'usable_count' => $this->integer()
                ->defaultValue(0),
            'created_at' => $this->integer()
        ], $tableOptions);

        $this->addForeignKey('fk_comment_parent', '{{%comment}}', 'parent_id', '{{%comment}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_comment_user', '{{%comment}}', 'user_id', '{{%user}}', 'id', 'cascade', 'cascade');
    }

    public function uninstall()
    {
        $this->dropForeignKey('fk_comment_user', '{{%comment}}');
        $this->dropForeignKey('fk_comment_parent', '{{%comment}}');
        $this->dropTable('{{%comment}}');
    }
}